<?php

/**
 * MVM_MALL 网上商店系统  商品属性管理
 * ============================================================================
 * 版权所有 (C) 2007-2010 www.mvmmall.com，并保留所有权利。
 * 网站地址: http://www.mvmmall.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * $Author:  www.mvmmall.com  $
 * $Date: 2008-07-03 $
 * $Id: attribute.inc.php  www.mvmmall.com$
 * ---------------------------------------------
*/

if(!defined('MVMMALL') || !defined('IN_ADMINCP')) exit('Access Denied');
//手动输入,下拉列表,多行文本框
$input_array = array(0=>$lang['manually_enter'],1=>$lang['down_list'],2=>$lang['text_box']);
if ($action=='list') {
    require_once MVMMALL_ROOT.'./include/pager.class.php';
    $search_sql = false;
    is_numeric($type_id) && $search_sql = "WHERE type_id='$type_id'";
    $total_count = $db->counter("{$tablepre}attribute",$scarch_sql);
    $page        = $page ? (int)$page:1;
    $list_num    = 10;
    $rowset      = new Pager($total_count,$list_num,$page);
    $from_record = $rowset->_offset();
    $result     = $db->query("SELECT a.id,a.type_id AS type_id,a.att_name,a.input_type,a.att_value,a.sort_order,b.uid,b.name FROM `{$tablepre}attribute` a  LEFT JOIN `{$tablepre}att_type` b ON a.type_id = b.uid $search_sql ORDER BY sort_order LIMIT $from_record, $list_num");
    while ($rt  = $db->fetch_array($result)){
        $rt['input_type'] = $input_array[$rt['input_type']];
        $rt['edit']       = "admincp.php?module=$module&action=edit&uid=$rt[id]";
        $rt['del']        = "admincp.php?module=$module&action=del&uid=$rt[id]";
        $att_rt[]         = $rt;
    }
    $result        = $db->query("SELECT * FROM {$tablepre}att_type");
    while ($rs     = $db->fetch_array($result)){
        $type_rt[] = $rs;
    }

    $page_list   = $rowset->link("admincp.php?module=$module&action=$action&type_id=$type_id&page=");
    $type_html   = drop_menu(type_arry(),'type_id',$type_id,'onchange="location.href=\'admincp.php?module=attribute&action=list&type_id=\' + (this.options[this.selectedIndex].value)"');
    require_once template('attribute');
    footer();
} elseif ($action=='add') {
    if($setp==1 && $_POST){
        $att_name    = dhtmlchars($att_name);
        $nums        = $db->counter("{$tablepre}attribute","where att_name='$name'");
        $nums >0     && admin_msg('already_exist');
        $type_id     = (int)$type_id;
        $input_type  = (int)$input_type;
        $att_type    = (int)$att_type;
        $sort_order  = (int)$sort_order;
        $att_value   = dhtmlchars($att_value);
        $query = "INSERT INTO `{$tablepre}attribute` SET
                  att_name    = '$att_name',
                  type_id     = '$type_id',
                  input_type  = '$input_type',
                  att_type    = '$att_type',
                  att_value   = '$att_value',
                  sort_order  = '$sort_order'
                 ";
        $db->query($query);
        admin_msg('success',"admincp.php?module=attribute&action=list&type_id=$type_id");
    } else {
       $type_html   = drop_menu(type_arry(),'type_id',$type_id);//类型下拉菜单
       require_once template('attribute_add'); 
       footer();
    }
} elseif ($action=='edit' && is_numeric($uid)) {
    if($setp==1 && $_POST){
        $att_name    = dhtmlchars($att_name);
        $nums        = $db->counter("{$tablepre}attribute","where att_name='$att_name' and id<>'$uid'");
        $nums >0     && admin_msg('already_exist');
        $type_id     = (int)$type_id;
        $input_type  = (int)$input_type;
        $att_type    = (int)$att_type;
        $sort_order  = (int)$sort_order;
        $att_value   = dhtmlchars($att_value);
        $query = "UPDATE {$tablepre}attribute SET
                  att_name    = '$att_name',
                  type_id     = '$type_id',
                  input_type  = '$input_type',
                  att_type    = '$att_type',
                  att_value   = '$att_value',
                  sort_order  = '$sort_order'
                  WHERE  id='$uid'";
        $db->query($query);
        admin_msg('success',"admincp.php?module=attribute&action=list&type_id=$type_id");
    } else {
       $att_rt      = $db->get_one("SELECT * FROM `{$tablepre}attribute` WHERE id = '$uid'");
       @extract($att_rt,EXTR_OVERWRITE);
       $type_html   = drop_menu(type_arry(),'type_id',$type_id);//类型下拉菜单
       $att_type==1 && $buy_chk = 'checked';
       switch ($input_type){
           case 0:  { $in_chk   = 'checked'; break;  }
           case 1:  { $sel_chk  = 'checked'; break;  }
           default: { $more_chk = 'checked'; break;  }
       }
       require_once template('attribute_add'); 
       footer();
    }
} elseif ($action=='del' && is_numeric($uid)) {
    $db->query("DELETE FROM `{$tablepre}att_value`  WHERE bute_id = '$uid'");
    $db->query("DELETE FROM `{$tablepre}attribute`  WHERE id = '$uid'");
    admin_msg('success',dreferer());
} else {
    admin_msg('pass_worng');
}

    /**
     * 商品属性数组
     * @access public
     * @vid  无
     * @@return array 返回下一维数组菜单
     */
function type_arry(){
    global $db,$tablepre;
    $type_rt = array();
    $result = $db->query("SELECT uid,name FROM `{$tablepre}att_type`");
    while ($rs= $db->fetch_array($result)){
        $type_rt[$rs['uid']]=$rs['name'];
    }
    return $type_rt;
}